package com.qf.java2201.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.qf.java2201.common.PageVo;
import com.qf.java2201.common.R;
import com.qf.java2201.dto.BackExcelDto;
import com.qf.java2201.listener.BackListener;
import com.qf.java2201.pojo.Back;
import com.qf.java2201.service.BackService;
import com.qf.java2201.utils.OssUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @author yjl
 * @version 1.0
 * @date 2022/4/26 14:31
 */
@RestController
@RequestMapping("/back")
//@CrossOrigin(origins = {"http://127.0.0.1:5500"}, allowCredentials = "true")
public class BackController {

    @Autowired
    private BackService backService;

    @RequestMapping("/backPageData")
    public PageVo<Back> getPageData(@RequestParam Map<String, Object> params, Back back) {
        return backService.getPageData(params, back);
    }

    @RequestMapping("/addback")
    public R addBack(Back back) {

        int result = backService.addback(back);

        R<Object> r = new R<>();

        if (result == 1) {
            r.setCode(0);
            r.setMsg("添加成功");
        } else {
            r.setCode(-1);
            r.setMsg("添加失败");
        }
        return r;
    }


    @RequestMapping("/deleteback")
    public R deleteBackById(Back back) {

        int result = backService.deleteBackById(back);
        R<Object> r = new R<>();
        if (result == 1) {
            r.setCode(0);
            r.setMsg("添加成功");
        } else {
            r.setCode(-1);
            r.setMsg("添加失败");
        }
        return r;
    }

    @RequestMapping("/updateback")
    public R updateBack(Back back) {

        int result = backService.updateBack(back);
        R<Object> r = new R<>();
        if (result == 1) {
            r.setCode(0);
            r.setMsg("添加成功");
        } else {
            r.setCode(-1);
            r.setMsg("添加失败");
        }
        return r;
    }

    @RequestMapping("/upload")
    public R uploadPic(MultipartFile file) throws IOException {
//        System.out.println(file.getOriginalFilename());
        String filename = file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();

        String s = OssUtil.uploadFile(inputStream, filename);

        R<String> r = new R();
        r.setCode(0);
        r.setMsg("上传成功");
        r.setData(s);
        return r;

//        return null;
    }

    @RequestMapping("/templateDownload")
    public void templateDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 读取服务器的模板文件 转为 字节数组
        String path = request.getServletContext().getRealPath("/template/反馈信息模板.xlsx");
        File file = new File(path);
        byte[] bytes = FileUtils.readFileToByteArray(file);

        //防止乱码
        String filename = URLEncoder.encode("反馈信息模板导出", "UTF-8");

        response.setHeader("Content-Disposition", "attachment;filename=" + filename + ".xlsx");
        ServletOutputStream outputStream = response.getOutputStream();

        outputStream.write(bytes);
        outputStream.close();
    }


    @RequestMapping("/export")
    public void export(Back back, HttpServletResponse response) throws IOException {
        //导出反馈信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //防止乱码
        String filename = URLEncoder.encode("客户反馈信息", "UTF-8");

        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        ServletOutputStream outputStream = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(outputStream, Back.class);

        ExcelWriterSheetBuilder sheet = write.sheet();

        //准备数据
        List<Back> backs = backService.backList(back);
        sheet.doWrite(backs);
    }

    @Autowired
    private BackListener backListener;
    @RequestMapping("/import")
    public R importData(MultipartFile file) throws IOException {
        //  上传Excel表格
        ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), BackExcelDto.class, backListener);
        ExcelReaderSheetBuilder sheet = read.sheet();
        sheet.doRead();
        return R.ok();

    }
}

